#include <stdio.h>
#include "A_memory.h"
#include "A_memory_shell.h"
#include "shell.h"


#define rt_uint32_t unsigned int
	
struct exception_info
{
    rt_uint32_t exc_return;
    rt_uint32_t r4;
    rt_uint32_t r5;
    rt_uint32_t r6;
    rt_uint32_t r7;
    rt_uint32_t r8;
    rt_uint32_t r9;
    rt_uint32_t r10;
    rt_uint32_t r11;
    rt_uint32_t r0;
    rt_uint32_t r1;
    rt_uint32_t r2;
    rt_uint32_t r3;
    rt_uint32_t r12;
    rt_uint32_t lr;
    rt_uint32_t pc;
    rt_uint32_t psr;
};

/*
 * fault exception handler
 */
void rt_hw_hard_fault_exception(struct exception_info * exception_info)
{
	unsigned int *app_sp;

	int i;
	app_sp = (unsigned int *)(exception_info + 1);  /* context + 16*4 */
	
    Ashell_print("psr: 0x%08x\r\n", exception_info->psr);
    Ashell_print("r00: 0x%08x\r\n", exception_info->r0);
    Ashell_print("r01: 0x%08x\r\n", exception_info->r1);
    Ashell_print("r02: 0x%08x\r\n", exception_info->r2);
    Ashell_print("r03: 0x%08x\r\n", exception_info->r3);
    Ashell_print("r04: 0x%08x\r\n", exception_info->r4);
    Ashell_print("r05: 0x%08x\r\n", exception_info->r5);
    Ashell_print("r06: 0x%08x\r\n", exception_info->r6);
    Ashell_print("r07: 0x%08x\r\n", exception_info->r7);
    Ashell_print("r08: 0x%08x\r\n", exception_info->r8);
    Ashell_print("r09: 0x%08x\r\n", exception_info->r9);
    Ashell_print("r10: 0x%08x\r\n", exception_info->r10);
    Ashell_print("r11: 0x%08x\r\n", exception_info->r11);
    Ashell_print("r12: 0x%08x\r\n", exception_info->r12);
    Ashell_print(" lr: 0x%08x\r\n", exception_info->lr);
    Ashell_print(" pc: 0x%08x\r\n", exception_info->pc);

	Ashell_print("stacks: \r\n");
	i = 0;
	for (i = 0; i < 1024; )
	{
		Ashell_print("%08x ", *app_sp);
		app_sp++;
		i++;
		if (i % 16 == 0)
			Ashell_print("\r\n");
			
	}
	Ashell_print("\r\n");

    while (1);
}
